package com.zhanghp.demo02;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhanghp
 * @date 2022-08-26 16:32
 */
public class EventManager {
    private Map<String, List<EventListener>> listeners;

    public EventManager(PhoneType ... phoneTypes) {
        this.listeners = new HashMap<>();
        for (PhoneType phone : phoneTypes) {
            this.listeners.put(phone.getTYPE(), new ArrayList<>());
        }
    }

    public void subscribe(PhoneType phoneType, EventListener listener){
        List<EventListener> eventListeners = listeners.get(phoneType.getTYPE());
        eventListeners.add(listener);
    }

    public void unSubscribe(PhoneType phoneType, EventListener listener){
        List<EventListener> eventListeners = listeners.get(phoneType.getTYPE());
        eventListeners.remove(listener);
    }

    public void notifyListener(PhoneType phoneType, PhoneStatus phoneStatus){
        List<EventListener> eventListeners = listeners.get(phoneType.getTYPE());
        for (EventListener listener : eventListeners) {
            listener.update(phoneType, phoneStatus);
        }
    }
}
